﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace GraphicsEditor
{
    public enum SelectedTool { selectToolRect, none, drawLine, drawRect, drawEllipse, brushTool, pencilTool, fillTool, textTool, selectToolPoly, selectToolElipse };

    public partial class ToolBox : DockContent
    {
        public static SelectTool selectTool = new SelectTool();
        public static ElipseSelectTool elipseSelectionTool = new ElipseSelectTool();
        public static PolySelectTool polySelectionTool = new PolySelectTool();

        public static DrawingTool drawTool = new DrawingTool();

        public static InvalidateDelegate invDel;

        private static int pixelSize = 1; // default

        public FontDialog fd = new FontDialog();

        private static Font tbfont = new Font("Times New Roman", 12); // default

        public static Font TBFont
        {
            get { return ToolBox.tbfont; }
            set { ToolBox.tbfont = value; }
        }

        public static int PixelSize
        {
            get { return ToolBox.pixelSize; }
            set { ToolBox.pixelSize = value; }
        }

        public static SelectedTool currentTool = SelectedTool.none;
        public static Color selectedColor = Color.White;
        public ToolBox()
        {
            InitializeComponent();

            selectTool = new SelectTool();
            elipseSelectionTool = new ElipseSelectTool();
            polySelectionTool = new PolySelectTool();
        }

        public static void setInvalidateDelegate(InvalidateDelegate del)
        {
            invDel = del;
        }

        private void btnNormal_Click(object sender, EventArgs e)
        {
            currentTool = SelectedTool.none;
            drawTool = new DrawingTool();
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            setSelectTool();
        }

        private ImageForm getActiveDocument()
        {
            ImageForm content = null;

            if (this.ParentForm.Text == "Image Editor Name")
            {
                //got correct parrent, cast it as the correct type and get a ref to the dockpanel
                content = (ImageForm)(((FormMainWindow)this.ParentForm).dockPanel.ActiveDocument);
            }

            return content;
        }

        private void btnDrawLine_Click(object sender, EventArgs e)
        {
            currentTool = SelectedTool.drawLine;
            drawTool = new DrawLine();
        }

        private void btnColor_Click(object sender, EventArgs e)
        {
            colorDialog.AllowFullOpen = true;
            if (colorDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                btnColor.BackColor = colorDialog.Color;
                selectedColor = btnColor.BackColor;
            }
        }

        private void DrawRect_Click(object sender, EventArgs e)
        {
            currentTool = SelectedTool.drawRect;
            drawTool = new DrawRect();
        }

        private void DrawEllipse_Click(object sender, EventArgs e)
        {
            currentTool = SelectedTool.drawEllipse;
            drawTool = new DrawEllipse();
        }

        private void lineSizeUpDown_ValueChanged(object sender, EventArgs e)
        {
            PixelSize = (int)lineSizeUpDown.Value;
        }

        private void BrushTool_Click(object sender, EventArgs e)
        {
            currentTool = SelectedTool.brushTool;
            drawTool = new BrushTool();
        }

        private void Pencil_Click(object sender, EventArgs e)
        {
            currentTool = SelectedTool.pencilTool;
            drawTool = new PencilTool();
        }

        private void FillTool_Click(object sender, EventArgs e)
        {
            currentTool = SelectedTool.fillTool;
            drawTool = new FillTool();
        }

        private void TextTool_Click(object sender, EventArgs e)
        {
            currentTool = SelectedTool.textTool;
            drawTool = new TextTool();
        }

        private void FontButton_Click(object sender, EventArgs e)
        {
            //if (fd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            fd.ShowDialog();
            TBFont = fd.Font;
        }

        private void setSelectTool()
        {
            ImageForm hndl = (ImageForm)(((FormMainWindow)this.ParentForm).dockPanel.ActiveDocument);
            if (rectangleSelectionToolStripMenuItem.Checked)
            {
                currentTool = SelectedTool.selectToolRect;
                if (hndl != null)
                {
                    polySelectionTool.deselect(hndl.pictureWindow);
                    elipseSelectionTool.deselect(hndl.pictureWindow);
                }
                drawTool = selectTool;
            }
            else if (elipseSelectionToolStripMenuItem.Checked)
            {
                currentTool = SelectedTool.selectToolElipse;
                if (hndl != null)
                {
                    polySelectionTool.deselect(hndl.pictureWindow);
                    selectTool.deselect(hndl.pictureWindow);
                }
                drawTool = elipseSelectionTool;
            }
            else if (polygonSelectionToolStripMenuItem.Checked)
            {
                currentTool = SelectedTool.selectToolPoly;
                if (hndl != null)
                {
                    elipseSelectionTool.deselect(hndl.pictureWindow);
                    selectTool.deselect(hndl.pictureWindow);
                }
                drawTool = polySelectionTool;
            }
        }

        private void selectCurrentTool()
        {
            switch (currentTool)
            {
                case SelectedTool.selectToolElipse:
                    btnSelect.Select();
                    break;
                case SelectedTool.selectToolPoly:
                    btnSelect.Select();
                    break;
                case SelectedTool.selectToolRect:
                    btnSelect.Select();
                    break;
                case SelectedTool.none:
                    btnNormal.Select();
                    break;
                case SelectedTool.drawRect:
                    DrawRect.Select();
                    break;
                case SelectedTool.drawLine:
                    btnDrawLine.Select();
                    break;
                case SelectedTool.drawEllipse:
                    DrawEllipse.Select();
                    break;
                case SelectedTool.brushTool:
                    Brush.Select();
                    break;
                case SelectedTool.pencilTool:
                    Pencil.Select();
                    break;
                case SelectedTool.fillTool:
                    FillButton.Select();
                    break;
                case SelectedTool.textTool:
                    TextButton.Select();
                    break;
                default:
                    btnNormal.Select();
                    break;
            }
        }

        private void selectToolContextMenu_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            if (e.ClickedItem == elipseSelectionToolStripMenuItem)
            {
                rectangleSelectionToolStripMenuItem.Checked = false;
                polygonSelectionToolStripMenuItem.Checked = false;
                elipseSelectionToolStripMenuItem.Checked = true;
                btnSelect.Image = GraphicsEditor.Properties.Resources.select_elipse;
            }
            else if (e.ClickedItem == rectangleSelectionToolStripMenuItem)
            {
                rectangleSelectionToolStripMenuItem.Checked = true;
                polygonSelectionToolStripMenuItem.Checked = false;
                elipseSelectionToolStripMenuItem.Checked = false;
                btnSelect.Image = GraphicsEditor.Properties.Resources.select;
            }
            else if (e.ClickedItem == polygonSelectionToolStripMenuItem)
            {
                rectangleSelectionToolStripMenuItem.Checked = false;
                polygonSelectionToolStripMenuItem.Checked = true;
                elipseSelectionToolStripMenuItem.Checked = false;
                btnSelect.Image = GraphicsEditor.Properties.Resources.select_poly;
            }
            else
            {
                Console.WriteLine("IMPOSSIBRU!");
            }
            btnSelect.Select();
            setSelectTool();
        }



    }
}
